<template>
  <div class="login_view">
    <div class="login-container">
      <div class="logo">
        <img src="/logo.png" alt="Your Company Logo" />
      </div>
      <h1>太平联动汽车服务</h1>
      <el-form size="large" label-position="top">
        <el-form-item label="账号">
          <el-input
            style="width: 350px"
            v-model="form.account"
            placeholder="请输入账号"
          />
        </el-form-item>
        <el-form-item label="密码" v-model="form.password">
          <el-input
            style="width: 350px"
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            show-password
            @keyup.enter="login"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            style="width: 100%"
            type="primary"
            size="large"
            @click="login"
            >登 录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import CryptoJS from "crypto-js";
import axios from "@/request";
import { useRouter } from "vue-router";

const router = useRouter();
const form = reactive({
  account: "",
  password: "",
});
const login = () => {
  // 加密密码
  const body = {
    account: form.account,
    password: encryptPassword(form.password),
  };
  axios
    .post(`/api/user/login`, body)
    .then((result) => {
      const { status, data } = result;
      if (status === 200) {
        sessionStorage.setItem("token", data.token);
        sessionStorage.setItem("account", form.account);
        router.push({
          path: "/",
        });
      }
    })
    .catch((err) => {});
};
// 加密函数
const encryptPassword = (password) => {
  return CryptoJS.MD5(password).toString();
};
</script>

<style scoped>
.login_view {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-image: url("/bg.jpg");
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
  /* 如果需要，可以添加背景颜色作为图片加载失败时的备选 */
  /* background-color: #f5f5f5; */
}

.login-container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: 400px;
  min-height: 300px;
  margin: 0;
  background-color: #ffffff;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
  padding: 30px;
  /* background-color: rgba(255, 255, 255, 0.8); */
}

.logo img {
  width: 100%;
  max-width: 50px;
  margin-bottom: 25px;
}

h1 {
  text-align: center;
  margin-bottom: 35px;
  color: #333;
}

@media (max-width: 767px) {
  .login-container {
    padding: 20px;
  }

  .logo img {
    max-width: 40px;
  }
}
</style>